<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('问卷设置') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('问卷名称')" prop="questionnaireName">
        <el-input
          v-model="queryParams.questionnaireName"
          :placeholder="$t('问卷名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['lp:questionnaireSet:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="lpQuestionnaireSetList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('问卷题目')" align="center" prop="questionnaireTitle" show-overflow-tooltip />
      <el-table-column :label="$t('问卷名称')" align="center" prop="questionnaireName" show-overflow-tooltip />
      <el-table-column :label="$t('问卷类型')" align="center" prop="questionnaireType" :formatter="questionnaireTypeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('对象类型')" align="center" prop="objectType" :formatter="objectTypeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('问卷版本')" align="center" prop="questionnaireVersion" show-overflow-tooltip />
      <!-- <el-table-column :label="$t('展现形式')" align="center" prop="representation" :formatter="representationFormat" show-overflow-tooltip /> -->
      <!--      <el-table-column :label="$t('当前问卷对应结果字典(字典标识)')" align="center" prop="resultDict" :formatter="resultDictFormat" />-->
      <el-table-column :label="$t('创建人')" align="center" prop="createUserName" show-overflow-tooltip />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['lp:questionnaireSet:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateQuestionHandle(scope.row)"
          >{{ $t('问卷题目维护') }}</el-button>
          <el-button
            v-hasPermi="['lp:questionnaireSet:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row,'result')"
          >{{ $t('问卷结果维护') }}</el-button>
          <el-button
            v-hasPermi="['lp:investigation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="inviteQuestions(scope.row)"
          >{{ $t('邀请答题') }}</el-button>
          <el-button
            v-hasPermi="['lp:questionnaireSet:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row,null)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['lp:questionnaireSet:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问卷设置对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <lpQuestionnaireSetForm ref="lpQuestionnaireSetForm" />
    </el-dialog>

    <!-- 添加或修改问卷题目维护对话框 -->
    <el-dialog :title="$t('问卷题目维护')" :visible.sync="questionOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="sysTypeTabs" append-to-body>
      <lpQuestionnaireQuestionList ref="lpQuestionnaireQuestionList" />
    </el-dialog>

    <el-dialog :title="$t('邀请答题')" :visible.sync="invitationOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <lpQuestionnaireInvitation ref="lpQuestionnaireInvitation" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listLpQuestionnaireSet,
  delLpQuestionnaireSet,
  exportLpQuestionnaireSet
} from '@/api/fund/questionnaire/settings/questionnaireSet/lpQuestionnaireSet'
import lpQuestionnaireSetForm from './lpQuestionnaireSetForm.vue'
import lpQuestionnaireQuestionList from '../questionnaireQuestion/lpQuestionnaireQuestionList'
import lpQuestionnaireInvitation
  from '@/views/fund/questionnaire/settings/questionnaireSet/lpQuestionnaireInvitation.vue'

export default {
  components: {
    lpQuestionnaireSetForm,
    lpQuestionnaireQuestionList,
    lpQuestionnaireInvitation
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 问卷设置表格数据
      lpQuestionnaireSetList: [],
      // 弹出层标题
      title: '问卷设置',
      // 是否显示弹出层
      formOpen: false,
      // 问卷题目弹出层
      questionOpen: false,
      // 邀请答题
      invitationOpen: false,
      // 问卷ID
      questionnaireId: undefined,
      // 问卷类型字典
      questionnaireTypeOptions: [],
      // 对象类型字典
      objectTypeOptions: [],
      // 展现形式字典
      representationOptions: [],
      // 当前问卷对应结果字典(字典标识)字典
      resultDictOptions: [],
      // 查询参数
      queryParams: {
        questionnaireName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDicts('questionnaire_type').then(response => {
      this.questionnaireTypeOptions = response.data
    })
    this.getDicts('questionnaire_object_type').then(response => {
      this.objectTypeOptions = response.data
    })
    this.getDicts('questionnaire_presentation_form').then(response => {
      this.representationOptions = response.data
    })
    this.getDicts('questionnaire_corresponding_results ').then(response => {
      this.resultDictOptions = response.data
    })
  },
  methods: {
    /** 查询问卷设置列表 */
    getList() {
      this.loading = true
      listLpQuestionnaireSet(this.queryParams).then(response => {
        this.lpQuestionnaireSetList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 问卷类型字典翻译
    questionnaireTypeFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.questionnaireTypeOptions, row.questionnaireType)
    },
    // 对象类型字典翻译
    objectTypeFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.objectTypeOptions, row.objectType)
    },
    // 展现形式字典翻译
    representationFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.representationOptions, row.representation)
    },
    // 当前问卷对应结果字典(字典标识)字典翻译
    resultDictFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.resultDictOptions, row.resultDict)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        questionnaireTitle: undefined,
        questionnaireName: undefined,
        questionnaireType: undefined,
        objectType: undefined,
        questionnaireVersion: undefined,
        representation: undefined,
        resultDict: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.lpQuestionnaireSetForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row, type) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.lpQuestionnaireSetForm.init(row, type)
      })
    },
    /** 问卷题目维护 */
    updateQuestionHandle(row) {
      this.questionOpen = true
      this.questionnaireId = row.id
      this.$nextTick(() => {
        this.$refs.lpQuestionnaireQuestionList.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delLpQuestionnaireSet(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
    },
    /** 邀请答题 */
    inviteQuestions(row) {
      console.log('邀请答题页面', row)
      this.invitationOpen = true
      this.$nextTick(() => {
        this.$refs.lpQuestionnaireInvitation.init(row)
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return exportLpQuestionnaireSet(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {
      })
    }
  }
}
</script>
